package top.aicats.blog.BlogManage.interceptor;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author sjc
 * @version 1.0
 * @date 2020/1/9 20:00
 * @email 952491025@qq.com
 */
public class RequestInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse respose, Object handler) {
        if (request.getMethod().equals("OPTIONS")) {
            respose.setStatus(HttpServletResponse.SC_OK);
            return true;
        } else {
            String authorization = request.getHeader("Authorization");
            if (authorization==null) {
                respose.setStatus(HttpServletResponse.SC_BAD_GATEWAY);
                return false;
            }
            else {
                System.out.println(authorization);
                return true;
            }
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        super.afterConcurrentHandlingStarted(request, response, handler);
    }


}
